// subPackage/view/user/setting/index.js
import {
  areaList
} from "@vant/area-data";
import Notify from '@vant/weapp/notify/notify';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {},
    areaList,
    showArea: false,
    sex: "male",
    showNickname: false,
    showLogout: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    this.setData({
      beforeClose: (action) => this.onBeforeClose(action)
    })
  },
  initData() {
    let userinfo = wx.getStorageSync('userinfo');
    if (Object.keys(userinfo).length > 0) {
      let expireTime = userinfo.expireTime;
      userinfo.expireTime = this.formatDate(expireTime);
      userinfo.expireDay = this.diffDate(expireTime);
      this.setData({
        userinfo: userinfo
      })
    }
  },
  formatDate(expireTime) {
    if (!expireTime) {
      return "";
    }
    let d = new Date(expireTime);
    let year = d.getFullYear();
    let month = d.getMonth() + 1;
    month = month > 9 ? month : ("0" + month);
    let day = d.getDate();
    day = day > 9 ? day : ("0" + day);
    let hour = d.getHours();
    hour = hour > 9 ? hour : ("0" + hour);
    let mintue = d.getMinutes();
    return year + "-" + month + "-" + day + " " + hour + ":" + mintue
  },
  diffDate(expireTime) {
    if (!expireTime) {
      return ""
    }
    let d = new Date(expireTime);
    let n = new Date();
    let diff = d.getTime() - n.getTime();
    return parseFloat(diff / (1000 * 60 * 60 * 24)).toFixed(0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  showArea() {
    this.setData({
      showArea: true
    })
  },
  showNickname() {
    this.setData({
      showNickname: true
    })
  },
  areaConfirm(e) {
    let areaCode = [];
    let areaName = [];
    let values = e.detail.values;
    for (let i = 0; i < values.length; i++) {
      areaCode.push(values[i].code);
      areaName.push(values[i].name);
    }
    let userinfo = this.data.userinfo;
    userinfo.areaCode = areaCode.join(" - ");
    userinfo.areaName = areaName.join(" - ");
    this.setData({
      userinfo,
      showArea: false
    });
    this.updateUser(userinfo);
  },
  changeSex(e) {
    let sex = this.data.sex;
    let userinfo = this.data.userinfo;
    userinfo.sex = sex;
    this.updateUser(userinfo);
  },
  onClose() {
    this.setData({
      showArea: false
    })
  },
  changeNickname() {

  },
  updateUser(userinfo) {
    app.request("user/update", "POST", userinfo, {
      "content-type": "application/json"
    }).then(res => {
      Notify({
        type: 'success',
        message: '请输入必修改成功填项',
        selector: '#notify_selector'
      })
      let userinfo = res.data;
      wx.setStorageSync('userinfo', userinfo);
      this.setData({
        userinfo
      });
      this.initData();
    })
  },
  onBeforeClose(action) {
    return new Promise((resolve) => {
      if (action === 'confirm') { // 点击确认
        let nickname = this.data.nickname;
        if (!nickname) {
          Notify({
            type: 'danger',
            message: '请输入昵称',
            selector: '#notify_selector'
          })
          resolve(false);
        } else {
          let userinfo = this.data.userinfo;
          userinfo.nickname = nickname;
          this.updateUser(userinfo);
          resolve(true);
          this.setData({
            showNickname: false
          })
        }
      } else { // 点击取消
        resolve(true)
        this.setData({
          showNickname: false
        })
      }
    })
  },
  toCard() {
    wx.switchTab({
      url: '/pages/cardsercet/index',
    })
  },
  logout() {
    this.setData({
      showLogout: true
    })
  },
  onLogout() {
    let userinfo = this.data.userinfo.id;
    app.request("user/logout", "POST", {
      id: userinfo.id
    }, "xform").then(res => {
      Notify({
        type: 'success',
        message: '登出成功',
        selector: '#notify_selector',
        onClose(){
          wx.removeStorageSync('userinfo')
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      })
     
    });
    wx.removeStorageSync('userinfo');
  }
})
